package com.n9.qqonline.sales.product;

import com.n9.qqonline.Context;
import com.n9.qqonline.api.PatentVO;

import java.util.List;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/5/30 14:56
 */
public class ServerLoader implements PatentLoader {
    @Override
    public void load(MsgDisplayer displayer) {
        List<PatentVO> vos = Context.API.getAllCanSell();
        if (Objects.isNull(vos)) {
            displayer.display("加载服务器专利失败");
            return;
        }
        MemoryPatentService memoryPatentService = new MemoryPatentService();
        memoryPatentService.clear();

        vos.forEach(vo ->{
            Patent patent = new Patent();
            patent.setNo(vo.getNo());
            patent.setName(vo.getName());
            patent.setStatus(vo.getStatus());
            patent.setExpireDay(vo.getExpireDate());
            patent.setSaleStatus(vo.getSaleStatus());
            patent.setPriority(vo.getPriority());

            String[] tagNames = vo.getTagNames();
            if (Objects.nonNull(tagNames) && tagNames.length > 0) {
                for (String tagName: tagNames) {
                    memoryPatentService.addPatent(tagName, patent);
                }
            } else {
                memoryPatentService.addPatent("", patent);
            }
        });

        displayer.display("加载远程专利成功,加载数量:" + vos.size());
    }
}
